package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.crm.domain.Customer;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * Created by xbqco on 2018/7/16.
 */
@Controller
public class FixedAreaController {

    @Autowired
    private FixedAreaService fixedAreaService;


//查询所有定区
    @RequestMapping("fixedArea_findAll")
    @ResponseBody
    public Map<String,Object> findAll(Integer page,Integer rows){
        Pageable pageable = new PageRequest(page-1,rows);
        Page<FixedArea> p= fixedAreaService.findAll(pageable);
        Map<String ,Object> map = new HashMap<>();
        map.put("total",p.getTotalElements());
        map.put("rows",p.getContent());
        List<FixedArea> list=p.getContent();
        return map;

    }
//保存或者更新定区
    @RequestMapping("/fixedArea_save")
    public String save( FixedArea fixedArea){

        if(StringUtils.isBlank(fixedArea.getId())){
            String uuid = UUID.randomUUID().toString();
            fixedArea.setId(uuid);
            System.out.println("新定区的id :"+uuid);
        }
        System.out.println("原定区的id :"+fixedArea.getId());
        fixedAreaService.update(fixedArea);

        return "redirect:/pages/base/fixed_area.html";
    }
//查找所有未关联到定区的用户
    @RequestMapping("fixedArea_findNoAssociationCustomers")
    @ResponseBody
    public Collection toCustomer(){
        Collection<? extends Customer> c= WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
                .accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
        return c;
    }
//查找关联到某定区的用户
    @RequestMapping("fixedArea_findHasAssociationFixedAreaCustomers")
    @ResponseBody
    public Collection toCustomerBYId(String id){
        if(StringUtils.isNotBlank(id)){
            return  WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+id)
                    .accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
        }
        return null;

    }
    //实现关联\取消关联定区
    @RequestMapping("fixedArea_associationCustomersToFixedArea")

    public String fixedArea_associationCustomersToFixedArea(String noCustomerIds[],String[] customerIds,FixedArea fixedArea){

        String customerIds1= StringUtils.join(customerIds,",");
        String noCustomerIds1=StringUtils.join(noCustomerIds,",");
        System.out.println(noCustomerIds1);

        WebClient.create("http://localhost:9002/crm_management/services/customerService/cancel?noCustomerIds="+noCustomerIds1
        ).post(null);

        WebClient.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="+customerIds1+"&&fixedAreaId="+fixedArea.getId()).post(null);

        return "redirect:/pages/base/fixed_area.html";
    }
    //查询关联到某一定区的快递员
    @RequestMapping("fixedArea_findHasAssociationFixedAreaCouriers")
    @ResponseBody
    public Map<String,Object> findHasAssociationFixedAreaCouriers(int page,int rows,String id){
        Pageable pageable = new PageRequest(page-1,rows);
        if(StringUtils.isNotBlank(id)){
            Page<Courier> p=fixedAreaService.findFixedAreaCouriers(pageable,id);
            Map map = new HashMap();
            map.put("total",p.getTotalElements());
            map.put("rows",p.getContent());
            System.out.println("关联:"+map.get("total"));
            return map;
        }
        return null;
    }
//查询没有关联到某一定区的快递员
    @RequestMapping("fixedArea_findNoAssociationCouriers")
    @ResponseBody
    public Map<String,Object> findNoAssociationCouriers(int page, int rows, String id){
        Pageable pageable = new PageRequest(page-1,rows);
        if(StringUtils.isNotBlank(id)){
            Page<Courier> p=fixedAreaService.findNoAssociationCouriers(pageable,id);
            Map map= new HashMap();
            map.put("rows",p.getContent());
            map.put("total",p.getTotalElements());
            System.out.println("未关联"+map.get("total"));
            return map;
        }
        return null;
    }
//关联到定区
    @RequestMapping("fixedArea_associationCourierToFixedArea")
    @ResponseBody
    public String associationCourierToFixedArea(String id,Integer[] courierIds){
        System.out.println("controller------------------------------"+courierIds.length);

        try {
            fixedAreaService.associationCourierToFixedArea(id,courierIds);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
    }
//取消关联到定区
    @RequestMapping("fixedArea_cancelAssociationCourierToFixedArea")
    @ResponseBody
    public String cancelAssociationCourierToFixedArea(String id,Integer[] courierIds){
        try {
            fixedAreaService.cancelAssociationCourierToFixedArea(id,courierIds);
            return "success";
        }catch(Exception e){
            e.printStackTrace();
            return "fail";
        }
    }




    //查找关联到某一定区的分区
}
